package com.app.controller;


import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.UserModel;
import com.app.model.VoucherRecordModel;
import com.app.service.AdminUserService;
import com.app.service.IntegralRecordService;
import com.app.service.UserService;
import com.app.service.VoucherRecordService;
import com.app.utils.TokenUtils;
import com.app.vo.VoucherRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 益联券记录表 前端控制器
 * @author 16693103420
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/voucherRecord")
public class VoucherRecordController {
    @Autowired
    private VoucherRecordService voucherRecordService;
    @Autowired
    private IntegralRecordService integralRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminUserService adminUserService;


    //用户查询积分使用记录
    @GetMapping("/getUserVoucherRecordList")
    public Result getUserVoucherRecordList(@RequestParam(defaultValue = "0") Integer page,
                                           @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();

        VoucherRecordVo voucherRecordVo = voucherRecordService.getUserVoucherRecordList(page, limit,userId);
        return Result.success(voucherRecordVo);
    }
    //根据id查询
    @GetMapping("/getvoucherRecordById")
    public Result getvoucherRecordById(Integer id) {
        VoucherRecordModel voucherRecord = voucherRecordService.getById(id);
        return Result.success(voucherRecord);
    }

    /**
     * 查询券、积分的记录（根据订单号、名称、手机号、时间范围）
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/getVoucherIntegralRecord")
    public Result getVoucherIntegralRecord(@RequestParam(required = false) String keyword,
                                           Integer ifVI,//1 券 2 积分 3 都要
                                           @RequestParam(required = false) String beginTime,
                                           @RequestParam(required = false) String endTime,
                                           Integer page,Integer limit,
                                           @RequestParam(required = false) Integer areaId,
                                           @RequestParam(required = false) String typeName,
                                           HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return voucherRecordService.getVoucherIntegralRecord(keyword,ifVI,beginTime,endTime,page,limit,areaId,user,typeName);
    }


    @GetMapping("/voucherRecordStatistics")
    public Result voucherRecordStatistics(@RequestParam(required = false) String keyword,
                                           Integer ifVI,//1 券 2 积分 3 都要
                                           @RequestParam(required = false) String beginTime,
                                           @RequestParam(required = false) String endTime,
                                           @RequestParam(required = false) Integer areaId,
                                          @RequestParam(required = false) String typeName,
                                           HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return voucherRecordService.voucherRecordStatistics(keyword,ifVI,beginTime,endTime,areaId,user,typeName);
    }









    /**
     * 券、积分查询详情
     * @param ifVI
     * @param id
     * @return
     */
    @GetMapping("/getVoucherOrIntegralById")
    public Result getVoucherOrIntegralById(Integer ifVI,Integer id){
        return voucherRecordService.getVoucherOrIntegralById(ifVI,id);
    }
/**
 * 券排行榜
 */
    @GetMapping("/voucherRanking")
    public Result voucherRanking(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if ("false".equals(userId)) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        Integer businessId=null;
        Integer cityId=null;
        Integer areaId=null;
        if (user.getRoleId() == 2) {
            businessId = user.getBusinessId();
        }
        if (user.getRoleId() == 3) {
            cityId = user.getAreaId();
        }
        if (user.getRoleId() == 4) {
            areaId = user.getAreaId();
        }
        return voucherRecordService.voucherRanking(businessId,cityId,areaId);
    }

    /**
     * 积分的排行榜
     * @return
     */
    @GetMapping("/integralRanking")
    public Result integralRanking(){
        return integralRecordService.integralRanking();
    }


    /**
     * 导出财务报表
     * @param roleId  角色id  3市代  4区代
     * @param response
     * @throws Exception
     */
    @GetMapping("/downLoadVoucherInfoExcel")
    public void downLoadVoucherInfoExcel(Integer roleId, HttpServletResponse response)throws Exception{
        voucherRecordService.downLoadVoucherInfoExcel(roleId,response);
    }

}

